<template>
  <div>
    <!-- 顶部操作区域 -->
    <div
      style="
        margin-top: 15px;
        width: 300px;
        margin-left: 45px;
        margin-bottom: 15px;
      "
    >
      <el-input
        placeholder="请输入医生关键字"
        v-model="query"
        class="input-with-select"
        :clearable="true"
        @blur="reList()"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getDoctorByName()"
        ></el-button>
      </el-input>
    </div>
    <!-- 表格部分 -->
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="servicerecord_id"
        label="记录编号"
        width="100%"
        align="center"
      >
      </el-table-column>
      <el-table-column fixed prop="order_number" label="订单号" align="center">
      </el-table-column>
      <el-table-column
        fixed
        prop="username"
        label="服务对象"
        width="100%"
        align="center"
      >
      </el-table-column>
      <el-table-column fixed prop="pet_name" label="宠物名称" align="center">
      </el-table-column>

      <el-table-column
        fixed
        prop="appointment_id"
        label="预约编号"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="service_name"
        label="项目名称"
        align="center"
      >
      </el-table-column>
      <el-table-column
        fixed
        prop="complete_time"
        label="完成时间"
        align="center"
      >
      </el-table-column>
      <el-table-column fixed prop="status" label="服务状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">完成</span>
          <span v-if="scope.row.status==2">院方拒绝</span>
          <span v-if="scope.row.status==3">用户取消</span>
          <span v-if="scope.row.status==4">已诊断</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button
            @click="editClick(scope.row)"
            type="primary"
            size="small"
            v-if="scope.row.is_medical == 1 && scope.row.status==1"
            >添加结果</el-button
          >
          <el-button
            v-show="scope.row.is_medical == 1 && scope.row.status==4"
            :disabled="true"
            type="success"
            size="small"
            >诊疗完毕</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-sizes="[5, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    ></el-pagination>

    <el-drawer
      title="医疗诊断"
      :visible.sync="drawer"
      :before-close="handleClose"
    >
      <div>
        <el-card class="box-card">
          <div class="text item">
            {{ "订单号：" + handle_obj.order_number }}
          </div>
          <div class="text item">{{ "预约用户：" + handle_obj.username }}</div>
          <div class="text item">{{ "宠物名称：" + handle_obj.pet_name }}</div>
          <el-form :model="form">
            <el-form-item label="诊断结果">
              <el-input type="textarea" v-model="form.diagnosis_result"></el-input>
            </el-form-item>
            <el-form-item label="是否用药">
              <el-switch v-model="form.is_medication_required"></el-switch>
            </el-form-item>
            <el-form-item label="用药详情：">
              <el-input
                type="textarea"
                v-model="form.medication_details"
              ></el-input>
            </el-form-item>
            <el-form-item label="医生建议：">
              <el-input type="textarea" v-model="form.doctor_advice"></el-input>
            </el-form-item>
            <el-button type="primary" @click="onSubmit">添加诊断</el-button>
          </el-form>
        </el-card>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { getAllDetails,addMedicalRecord,updateServiceRecord } from "@/utils/request";
export default {
  name: "DyymanagevueServiceRecords",

  data() {
    return {
        query:"",
      drawer: false,
      tableData: [],
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页显示条数
      totalItems: 0, // 总记录数
      handle_obj: "", //操作对象
      form: {
        diagnosis_result: "",
        is_medication_required: 0,
        medication_details: "",
        doctor_advice: "",
      },
    };
  },

  mounted() {
    this.getList();
  },

  methods: {
    async getList() {
      await getAllDetails()
        .then((res) => {
          this.tableData = res.data.data;
        })
        .catch((error) => {
          // 处理错误
          console.error(error);
        });
    },
    reList() {
      this.getList();
    },
    editClick(operator) {
      this.drawer = true;
      this.handle_obj = operator;
    },
    onSubmit(){
        //创建需要对象数据
        let data = {
            order_number:this.handle_obj.order_number,
            user_id:this.handle_obj.user_id,
            diagnosis_result: this.form.diagnosis_result,
            is_medication_required: this.form.is_medication_required?1:0,
            medication_details: this.form.medication_details,
            doctor_advice: this.form.doctor_advice
        }
        //发起插入请求
        addMedicalRecord(data).then((res)=>{
           if(res.data.status==true){
            //插入成功后发起修改状态的操作
            updateServiceRecord(this.handle_obj.servicerecord_id).then((res)=>{
              this.reList()
            })
           }
        })
    },
    handleClose(done) {
      //   this.$confirm("确认关闭？")
      //     .then((_) => {

      //     })
      //     .catch((_) => {});
      done();
      this.handle_obj = "";
    },
    // 分页器页码变化事件处理函数
  handleCurrentChange(newPage) {
    this.currentPage = newPage; // 更新当前页码
  },
  handleSizeChange(newSize) {
    this.pageSize = newSize; //更新每页数据条数
  },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  position: absolute;
  bottom: 0;
  left: 45%;
  /* width: 30%; */
  bottom: 5%;
  background-color: #fff;
  z-index: 999; /* 可以根据需要设置z-index值，确保分页器显示在其他内容上方 */
  box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.1); /* 添加阴影效果（可选） */
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
</style>